<?php

declare(strict_types=1);

namespace App\Middleware;

use Hyperf\Context\Context;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use App\Service\SysLog as SysLogService;

class LogMiddleware implements MiddlewareInterface
{
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        $method = $request->getMethod();


        if ($method != 'GET') {
            $header = $request->getHeaders();
            $data = [
                'uid' => 1,
                'ip' => isset($header['x-real-ip'][0]) ? $header['x-real-ip'][0] : '',
                'host' => isset($header['host'][0]) ? $header['host'][0] : '',
                'method' => $method,
                'url' => $request->getUri()->getPath(),
                'params' => $request->getParsedBody(),
            ];
            (new SysLogService())->addLog($data);
        }
        return $handler->handle($request);
    }
}
